﻿// Copyright (c) 2012 - 2021 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2021-03-19 version 4.0.0 Jared Zheng <jaredz at outlook dot com>

#ifndef __COMMON_PACKET_H__
#define __COMMON_PACKET_H__

#pragma once

#include "PacketDefine.h"
#include "PacketStream.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 服务通用信令
enum PAK_EVENT_COMMON
{
    // 心跳包
    PAKE_LIVE = CNETTraits::EVENT_RESERVED,
    // 心跳包Ack
    PAKE_LIVEACK,

    // 注册
    PAKE_LINK,
    // 注册Ack
    PAKE_LINKACK,

    // 更新
    PAKE_UPDATE,
    // 更新Ack
    PAKE_UPDATEACK,

    // 同步
    PAKE_SYNC,
    // 同步Ack
    PAKE_SYNCACK,

    // 注销
    PAKE_UNLINK,
    // 注销Ack
    PAKE_UNLINKACK,

    // event-queue used only
    PAKE_TCPLISTEN,
    PAKE_TCPLISTEN_CLOSE,

    PAKE_TCPCONNECT,
    PAKE_TCPCONNECT_CLOSE,

    PAKE_TCPACCEPT,
    PAKE_TCPACCEPT_CLOSE,

    PAKE_UDPCREATE,
    PAKE_UDPCLOSE,

    PAKE_QUEUESTART,
    PAKE_QUEUESTOP,
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CTPAKStream
template <typename TPAK>
class CTPAKStream : public CTPAKStreamBase<TPAK>
{
public:
    CTPAKStream(void);
    CTPAKStream(UEVENT uEvent, URANK uRank, USID uSid = 0, UDATA uData = 0);
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通用数据包
typedef CTPAKStream<CPAKHeadBase>    CNETHead;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通用同步数据包
class CPAKSync : public CPAKHeadBase
{
public:
    CPAKSync(UEVENT uEvent = 0, URANK uRank = (URANK)COMMONR_MASTER, USID uSid = 0, UDATA uData = 0);
    virtual ~CPAKSync(void);

    CPAKSync(const CPAKSync& aSrc);
    CPAKSync& operator=(const CPAKSync& aSrc);

    virtual size_t  Length(void) const OVERRIDE;
    virtual void    Serialize(CStream&) OVERRIDE;

    SERVER_STATUS&  GetStatus(void);
    void            SetStatus(SERVER_STATUS& ss);
private:
    SERVER_STATUS   m_Status;
};

typedef CTPAKStream<CPAKSync>    CNETSync;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通用ID数据包
class CPAKID : public CPAKHeadBase
{
public:
    CPAKID(UEVENT uEvent = 0, URANK uRank = (URANK)COMMONR_MASTER, USID uSid = 0, UDATA uData = 0);
    virtual ~CPAKID(void) OVERRIDE;

    CPAKID(const CPAKID& aSrc);
    CPAKID& operator=(const CPAKID& aSrc);

    virtual size_t Length(void) const OVERRIDE;
    virtual void   Serialize(CStream&) OVERRIDE;

    ULLong         GetId(void);
    void           SetId(ULLong ullId);
private:
    ULLong         m_ullId;
};
typedef CTPAKStream<CPAKID>    CNETID;
typedef CTRefCountPtr<CNETID>  CNetIdPtr;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通用SRV数据包
class CPAKSRV : public CPAKID
{
public:
    CPAKSRV(UEVENT uEvent = 0, URANK uRank = (URANK)COMMONR_MASTER, USID uSid = 0, UDATA uData = 0);
    virtual ~CPAKSRV(void) OVERRIDE;

    CPAKSRV(const CPAKSRV& aSrc);
    CPAKSRV& operator=(const CPAKSRV& aSrc);

    virtual size_t Length(void) const OVERRIDE;
    virtual void   Serialize(CStream&) OVERRIDE;

    ULLong         GetSrvId(void);
    void           SetSrvId(ULLong ullSrvId);
private:
    ULLong         m_ullSrvId;
};
typedef CTPAKStream<CPAKSRV>    CNETSRV;
typedef CTRefCountPtr<CNETSRV>  CNetSrvPtr;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 通用数据包解析
namespace CommonPacket
{
    static CPAKHeadBase* Create(CStream&, UEVENT& uEvent);
    static bool ReadString(CStream& Stream, CString& str, SizeLen sMaxLen = LMT_KEY);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CommonPacket.inl"

#endif // __COMMON_PACKET_H__
